import router from './router'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

router.beforeEach((to, from, next) => {
  NProgress.start()
  const ids = localStorage.getItem('IDS')
  const user = localStorage.getItem('USER')
  const token = localStorage.getItem('TOKEN')
  const expires_in = localStorage.getItem('expires_in')
  const EXPIRESTIME = 345600000 //定的4天，后台定的5天，为了以防万一，就提早一天
  // const whiteList = ['/login','/register' ]
  const whiteList = ['/login','/register','/donation','/about' ]
  /*
      已经采用了login，register，home，rewards，goods，pay，my
      guide(显示文章的),recharge（充值）,wallet(充值列表),withdrawallist(提现列表),account(财务列表),headimg(头像)
      profile(设置列表)
      saveusername(修改用户昵称)
      Saveuserbank(修改用户银行卡信息)
  */

  if (user && token && ids) {

    let date = new Date().getTime();
    if (date - expires_in > EXPIRESTIME) {
      localStorage.clear()
      if (whiteList.indexOf(to.path) !== -1) {
        next()
      } else {
        //这个地方是控制是否登录的
        next()
        // next(`/login?redirect=${to.path}`)
        NProgress.done()
      }
    } else {
      next()
    }

  } else {
    // 清理本地缓存
    localStorage.clear()
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      //这个地方是控制是否登录的
      next()
      // next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
